package ws.jakarta.ibukota.culinary.dao.impl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

import ws.jakarta.ibukota.culinary.dao.FoodItemDao;
import ws.jakarta.ibukota.culinary.model.FoodItem;

/**
 * @author Landung Wahana
 *
 */
public class FoodItemDaoImpl implements FoodItemDao {
	@Autowired
	SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	/* (non-Javadoc)
	 * @see ws.jakarta.ibukota.culinary.dao.FoodItemDao#getId(java.lang.String)
	 */
	public FoodItem getById(final String id) {
		return (FoodItem)sessionFactory.getCurrentSession(). createQuery("from FoodItem fi where fi.id =:itemId").setParameter("itemId",id).uniqueResult();
	}

	/* (non-Javadoc)
	 * @see ws.jakarta.ibukota.culinary.dao.FoodItemDao#getAllItems()
	 */
	@SuppressWarnings("unchecked")
	public List<FoodItem> getAllItems() {
		
		return sessionFactory.getCurrentSession().createQuery("from FoodItem").list();
	}

	
	
//	/* (non-Javadoc)
//	 * @see ws.jakarta.ibukota.culinary.dao.FoodItemDao#getId(java.lang.String)
//	 */
//	public FoodItem getById(final String id) {
//		return (FoodItem) getJpaTemplate().execute(new JpaCallback<Object>() {
//
//			public Object doInJpa(EntityManager em) throws PersistenceException {
//				Query q = em.createNamedQuery("findFoodItemById");
//				q.setParameter("id", id);				
//				return q.getSingleResult();
//			}
//
//		});
//	}
//
//
//	/* (non-Javadoc)
//	 * @see ws.jakarta.ibukota.culinary.dao.FoodItemDao#getAllItems()
//	 */
//	@SuppressWarnings("unchecked")
//	public List<FoodItem> getAllItems() {
//		return (List<FoodItem>) getJpaTemplate().executeFind(new JpaCallback<Object>() {
//
//			public Object doInJpa(EntityManager em) throws PersistenceException {
//				Query q = em.createNamedQuery("findAllFoodItems");
//				return q.getResultList();
//			}
//			
//		});
//	}
	
	

}
